import 'package:enterprise_v8/app/components/custom_app_bar.dart';
import 'package:enterprise_v8/app/core/core.dart';
import 'package:enterprise_v8/generated/assets.gen.dart';
import 'package:enterprise_v8/global.dart';
import 'version_info_controller.dart';

/// 版本信息页面
class VersionInfoPage extends StatelessWidget {
  VersionInfoPage({super.key});

  final controller = Get.put(VersionInfoController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(titleText: '版本信息'),
            body: Container(
              alignment: Alignment.center,
              child: Column(
                children: [
                  Container(height: 10.w, color: Colors.grey[100]),
                  // 信息部分
                  _getInfoView(),
                  _getDivider(),
                  // 功能部分
                  _getFunctionView(),
                ],
              ),
            ),
          );
        });
  }

  /// 信息部分
  Widget _getInfoView() {
    return Expanded(
        flex: 1,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 应用图标
            ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: SizedBox(
                width: 70.w,
                height: 70.w,
                child: Assets.appLogo.image(),
              ),
            ),
            AppGaps.vGap24,
            // 应用名称
            Text(
              Global.packageInfo.appName,
              style: TextStyle(
                color: const Color(0xFF000000),
                fontSize: 20.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ));
  }

  /// 页面分割线
  Widget _getDivider() {
    return Divider(height: 0.5.w, color: Colors.grey[100]).marginSymmetric(horizontal: 10.w);
  }

  /// 列表页面
  Widget _listView(String title, Function() onTap, {Widget? trailing}) {
    return Column(
      children: [
        ListTile(
          title: Text(
            title,
            style: TextStyle(
              color: Colors.black,
              fontSize: 16.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
          onTap: onTap,
          trailing: trailing ??
              Icon(
                Icons.arrow_forward_ios,
                size: 16,
                color: Colors.grey[500],
              ),
        ),
        _getDivider(),
      ],
    );
  }

  /// 功能部分
  Widget _getFunctionView() {
    return Expanded(
      flex: 2,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: SingleChildScrollView(
          child: Column(
            children: [
              _listView(
                '当前版本',
                trailing: Text(
                  'Version ${Global.packageInfo.version}',
                  style: TextStyle(
                    color: const Color(0xFF8791AA),
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                () {},
              ),
              _listView('检查更新', controller.checkUpdate),
              _listView('功能介绍', () {}),
            ],
          ),
        ),
      ),
    );
  }
}
